---
import Default from '@astrojs/starlight/components/Pagination.astro';
import type { Props } from '@astrojs/starlight/props';
import { tutorialPages as pages } from '~/content';
import { getTutorialPages } from '../../util/getTutorialPages';
import { getLangFromSlug, stripLangFromSlug } from '~/util';

const { entry, slug, pagination } = Astro.props;

// @ts-expect-error type isn't specific enough, but we know it's correct
const lang = getLangFromSlug(slug);
let { prev, next } = pagination;

const tutorialPages = getTutorialPages(pages, lang.toLowerCase());

if (Astro.props.entry.slug.split('/')[1] === 'tutorial') {
	const i = tutorialPages.findIndex((p) => p.slug === entry.slug);
	if (tutorialPages[i - 1]) {
		const prevPage = tutorialPages[i - 1];

		// @ts-expect-error type is incorrect?
		prev = {
			href: `/${lang}/${stripLangFromSlug(prevPage.slug)}/`,
			isCurrent: false,
			label: prevPage.data.title,
			type: 'link',
		};
	}
	if (tutorialPages[i + 1]) {
		const nextPage = tutorialPages[i + 1];

		// @ts-expect-error type is incorrect?
		next = {
			href: `/${lang}/${stripLangFromSlug(nextPage.slug)}/`,
			isCurrent: false,
			label: nextPage.data.title,
			type: 'link',
		};
	}
}
---

<Default {...{ ...Astro.props, ...{ pagination: { prev, next } } }} />
